<?php

function emailBooking ($booking, $comment, $user) {

    $team = array();
    $players = array();
    $customerList = array("","");
    $team[0] = get_field('team1', $booking);
    $team[1] = get_field('team2', $booking);

    $players[0] = json_decode(get_field('players', $team[0]), true);
    $players[1] = json_decode(get_field('players', $team[1]), true);

    foreach ($players[0] as $key => $value) {
        $customerList[0] = $customerList[0]."$value[name] <$value[email]>,";
    }
    foreach ($players[1] as $key => $value) {
        $customerList[1] = $customerList[1]."$value[name] <$value[email]>,";
    }

    $date = get_field('date',$booking);
    $slot = get_field('slot',$booking);
    $pitch = getPitchID($booking);
    $venue = getVenueID($booking);

    $user_info = get_userdata($user);
    $subject = 'Your Football Game Has Been Booked!';
    $message = '
    <html>
    <head>
    <title></title>
    </head>
    <body>
    <p>Great news, '.$user_info->user_firstname.' has used <a href="http://www.pitchwise.co.uk">Pitchwise</a> to organise your next game and all the details are below.</p>
    <p>We are a new company that aims to help you search and book the best pitch at the best price because we know that playing is the best bit! Oh and before we go, have a look for yourself and keep in the loop by leaving us your details or just letting us know what you think. Your contribution is important.</p>
    <p>Note: You can REPLY ALL from this email</p>
    <p><strong>Kick-Off</strong>: ' . date('l, j F Y', strtotime($date)) . ' at ' . get_field('start_time', $slot) . '</p>
    <p><strong>Where</strong>: <a href="'.get_permalink($pitch).'">'.get_field('pid',$pitch).'</a> - '.get_field('name',$venue).', '.get_field('address',$venue).'</p>
    <p><strong>Nearest Station(s)</strong>: '.get_field('nearest_station',$venue).'</p>
    <p><strong>Size</strong>: '.get_field('size',$pitch).'</p>
    <p><strong>Surface</strong>: '.get_field('surface',$pitch).'</p>

    <hr><strong>'.get_field('name', $team[0]).' Lineup</strong>
    <table style="margin-top:15px;">
    <thead><tr style=""><th style="width:150px;">Team Colour</th><th style="width:150px;">Player Name</th></tr></thead>
    <tbody>';
    foreach ($players[0] as $key => $value) {
        $message = $message.'<tr><td style="text-align:center;"><img src="http://dev.pitchwise.co.uk/wp-content/themes/pitchwise/img/T-'.get_field('colour', $team[0]).'.png" style="width:40px;height:40px;"></td>';
        $message = $message."<td style='text-align:center;'>$value[name]</td></tr>";
    }
    $message = $message.'</tbody></table>';

    $message = $message.'<hr><strong>'.get_field('name', $team[1]).' Lineup</strong>
    <table style="margin-top:15px;">
    <thead><tr style=""><th style="width:150px;">Team Colour</th><th style="width:150px;">Player Name</th></tr></thead>
    <tbody>';
    foreach ($players[1] as $key => $value) {
      $message = $message.'<tr><td style="text-align:center;"><img src="http://dev.pitchwise.co.uk/wp-content/themes/pitchwise/img/T-'.get_field('colour', $team[1]).'.png" style="width:40px;height:40px;"></td>';
      $message = $message."<td style='text-align:center;'>$value[name]</td></tr>";
    }
    $message = $message.'</tbody></table>';

    $message = $message.'<hr><strong>Comments from '.$user_info->user_firstname."</strong><p>$comment</p></body></html>";
    $headers = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    $headers .= 'To: ' . $user_info->user_firstname . ' <' . $user_info->user_email . '>' . "\r\n";
    $headers .= 'Cc: ' . $customerList[0] . $customerList[1] . "\r\n";
    $headers .= 'From: PitchWise <myteam@pitchwise.co.uk>' . "\r\n";
    //$headers .= 'Bcc: nazzanuk@gmail.com, nathan@pitchwise.co.uk, avneet@pitchwise.co.uk, kumar@pitchwise.co.uk, customer@pitchwise.co.uk' . "\r\n";
    $headers .= 'Bcc: nazzanuk@gmail.com' . "\r\n";

    mail($to, $subject, $message, $headers);


    return array('players'=> $players, 'customerList'=> $customerList);

}